﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Check_Printing_Solution
{
    public partial class ManageChequeTypes : Form
    {
        Form formsender = null;
        PrintManager manager = new PrintManager();
        List<ChequeType> chequetypes = new List<ChequeType>();

        public ManageChequeTypes()
        {
            InitializeComponent();
        }

        private void ManageChequeTypes_Load( object sender, EventArgs e )
        {
            RefreshChequeTypes();
        }

        private void RefreshChequeTypes()
        {
            dgChequeTypes.DataSource = manager.GetAllChequeTypes();

            dgChequeTypes.Columns["Id"].Visible = false;
            dgChequeTypes.Columns["Layouts"].Visible = false;
            dgChequeTypes.Columns["UseForAll"].Visible = false;
            dgChequeTypes.Columns["Description"].Width = 400;
        }

        private void btnFinish_Click( object sender, EventArgs e )
        {
            formsender.Show();
            this.Close();
        }

        private void btnNewSave_Click( object sender, EventArgs e )
        {
            if ( txtNewName.Text.Trim() != string.Empty )
            {
                ChequeType chequetype = new ChequeType();
                chequetype.Id = Guid.NewGuid().ToString();
                chequetype.Name = txtNewName.Text.Trim();
                chequetype.Description = txtNewDesctiption.Text.Trim();
                manager.AddChequeType( chequetype );

                txtNewDesctiption.Text = "";
                txtNewName.Text = "";

                RefreshChequeTypes();

                MessageBox.Show( "Cheque Type " + chequetype.Name + " added successfully", "Cheque Type Added", MessageBoxButtons.OK, MessageBoxIcon.Information );
            }
            else
            {
                MessageBox.Show( "Please add a name for the Cheque Type before saving.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
            }
        }

        private void btnUpdate_Click( object sender, EventArgs e )
        {
            if ( txtNewNameU.Text.Trim() != string.Empty )
            {
                ChequeType chequetype = new ChequeType();
                chequetype.Id = dgChequeTypes.SelectedRows[0].Cells["Id"].Value.ToString();
                chequetype.Description = txtNewDesctiptionU.Text.Trim();
                chequetype.Name = txtNewNameU.Text.Trim();

                manager.UpdateChequeTypes( chequetype );
                RefreshChequeTypes();

                MessageBox.Show( "Cheque Type " + chequetype.Name + " updated successfully", "Cheque Type Updated", MessageBoxButtons.OK, MessageBoxIcon.Information );
            }
            else
            {
                MessageBox.Show( "Please add a name for the Cheque Type before saving.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
            }
        }

        private void button1_Click( object sender, EventArgs e )
        {
            this.Close();
        }

        private void dgChequeTypes_SelectionChanged( object sender, EventArgs e )
        {
            if ( dgChequeTypes.SelectedRows.Count != 0 )
            {
                txtNewNameU.Text = dgChequeTypes.SelectedRows[0].Cells["Name"].Value.ToString();
                txtNewDesctiptionU.Text = dgChequeTypes.SelectedRows[0].Cells["Description"].Value.ToString();
            }
        }

        private void btnDelete_Click( object sender, EventArgs e )
        {
            if ( MessageBox.Show( "Are you sure you want to delete this Cheque Type?", "Delete Cheque Type", MessageBoxButtons.YesNo, MessageBoxIcon.Question ) == System.Windows.Forms.DialogResult.Yes )
            {
                ChequeType chequetype = new ChequeType();
                chequetype.Id = dgChequeTypes.SelectedRows[0].Cells["Id"].Value.ToString();
                manager.DeleteChequeType( chequetype );
                RefreshChequeTypes();

                MessageBox.Show( "Cheque Type " + chequetype.Name + " deleted successfully", "Cheque Type Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information );
            }
        }

        private void cmbBanks_SelectedIndexChanged( object sender, EventArgs e )
        {
            RefreshChequeTypes();
        }
    }
}
